#ifndef NETWORKMANAGE_H
#define NETWORKMANAGE_H

#pragma once

#include "portmanage.h"
#include <QUdpSocket>
#include <QTcpSocket>

namespace ns_yht
{

#define NETWORKMANAGE NetworkManage::instance()

    class NetworkManage : public PortManage
    {
        Q_OBJECT
    private:
        NetworkManage(QObject *parent = nullptr);

    public:
        ~NetworkManage();

        static NetworkManage *instance();

        QUdpSocket *getUdpSocket() const;
        QTcpSocket *getTcpSocket() const;
        bool isUdpOpened() const;
        bool isTcpOpened() const;
        QString getPortsSetting() const override;
        void setPortsSetting(const QString &portsSetting) override;
        void moveToThreadEx(QThread *thread) override;

    public slots:
        void openUdpSocket(int localPort);
        void openTcpSocket(const QString &server, int remotePort);
        void closeUdpSocket();
        void closeTcpSocket();

    private slots:
        void onReadyRead_tcp();
        void onReadyRead_udp();

    signals:
        void newSentence(QString isUdp, const QByteArray &sentence);
        void udpChanged(bool opened);
        void tcpChanged(bool opened);

    private:
        void checkTcpSocket();
        static NetworkManage *g_instance;
        QUdpSocket *m_udpSocket{nullptr};
        QTcpSocket *m_tcpSocket{nullptr};

        QString m_tcpServer;
        int m_tcpPort{0};
    };

} // namespace ns_yht
#endif